package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.core.utils.RequestUtil;
import com.weixing.mall.provider.mapper.NewsMapper;
import com.weixing.mall.provider.model.domain.News;
import com.weixing.mall.provider.model.dto.cms.NewsDto;
import com.weixing.mall.provider.model.enums.cms.NewsStatusEunm;
import com.weixing.mall.provider.model.enums.cms.NewsTypeEnum;
import com.weixing.mall.provider.model.query.NewsQuery;
import com.weixing.mall.provider.model.vo.cms.NewsEditVo;
import com.weixing.mall.provider.model.vo.cms.NewsFrontVo;
import com.weixing.mall.provider.model.vo.cms.NewsVo;
import com.weixing.mall.provider.service.INewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * CMS模块-公告资讯表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Override
    public void insert(NewsDto newsDto) {

    }

    @Override
    public IPage<NewsVo> selectOfpage(Page page, NewsQuery newsQuery) {
        return baseMapper.selectListOfPage(page,newsQuery);
    }

    @Override
    public void save(NewsDto newsDto,Long uid) {
        News news = new News();
        BeanUtils.copyProperties(newsDto,news);
        news.setStatus(newsDto.getStatus().getValue());
        if(NewsStatusEunm.HAS_PUB.getValue().equals(newsDto.getStatus().getValue())){
            news.setPubTime(new Date());
        }
        if(null==newsDto.getId()){
            news.setCreatedBy(uid);
            news.setCreatedTime(new Date());
            save(news);
        }else {
            news.setCreatedBy(uid);
            news.setUpdatedTime(new Date());
            updateById(news);
        }

    }

    @Override
    public NewsEditVo selectNewsEditVoById(long id) {
        return baseMapper.selectNewsEditVoById(id);
    }

    @Override
    public IPage<NewsFrontVo> selectNewsFrontVoList(Page page, NewsTypeEnum type) {
        return baseMapper.selectNewsFrontVoList(page,type);
    }
}
